<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    
    <title>问卷调查内容管理</title>
    
	<link rel="stylesheet" type="text/css" href="<%=path%>/JS/extjs/resources/css/ext-all.css"/>
	<script type="text/javascript" src="<%=path%>/JS/extjs/adapter/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/JS/extjs/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/JS/extjs/ext-lang-zh_CN.js"></script>
	<script type="text/javascript" src="<%=path%>/JS/jquery-1.7.2.js"></script>
	
	<script type="text/javascript">
	
	</script>

  </head>
  
  <body>
    <script type="text/javascript">
  	//选中行的ID
	var qTitleId = "";//选择行的标题ID
	var showId = "";//选择行的show_id
	var store = null;
	var proxy = null;
	var win = null;
	var data = null;
	var showInfoStore = null;//展会store
	var up_qTitleStore = null;//弹框中的标题store
	var type;
	var grid = null;//列表
	
	function deleteTermQ(selIds){
		//alert(selIds);
		Ext.MessageBox.confirm("提示","确定删除？",function(btn){
			if(btn=="yes"){
				if(qTitleId == ""){
					Ext.Msg.alert("提示","请选择删除行");
				}else if(qTitleId != ""){
					Ext.Ajax.timeout = 3 * 60 * 1000;
	        		Ext.Ajax.request({
	        		       url: '${pageContext.request.contextPath}/qContentAction!deleteQContent.action',
	        		       method : 'POST',
	        		       params:{selIds:selIds},
	        			   success: function(response, options){
		        		   		var resultjson = Ext.util.JSON.decode(response.responseText);
		        		   		//store.reload();
	            			   	Ext.Msg.alert("提示","操作成功");
	            			   	reloadQContentList();
	        		       },
	        			   failure : function(request){   
	        		           Ext.MessageBox.show({   
	        		                   title : '提示',   
	        		                    msg : "操作失败",   
	        		                    buttons : Ext.MessageBox.OK,   
	        		                    icon : Ext.MessageBox.ERROR   
	        		                    });   
	        		       }
	        		  });
				}
			}
		});
	}
	
	//保存
	function saveQTitle(){
		var formPanel = Ext.getCmp("formPanel");
		var formvalue = Ext.util.JSON.encode(formPanel.getForm().getValues());
	    //alert(formvalue);
		if(formPanel.getForm().isValid()){
			formPanel.getForm().submit({
				 	url : '${pageContext.request.contextPath}/qContentAction!saveQContent.action',
				 	contentType: "application/x-www-form-urlencoded; charset=utf-8",
				 	params:{formStr:encodeURIComponent(formvalue)},
                    success : function(from, action) {
						Ext.Msg.alert('提示', '保存成功！',function(){
							win.close();
							//store.reload();
							reloadQContentList();
						});
                    },
                    failure : function(form, action) {
                    	Ext.Msg.alert('提示', '保存失败！');
                    },
                    submitEmptyText: false,
                    waitMsg : '正在保存数据，稍后...'
			});
		}else{
			Ext.Msg.alert("提示","输入有错误");
		}
	}
	
	//根据标题重新加载内容列表
	function reloadQContentList(){
		//当前选择的机构类的id
		var qTitleID = Ext.getCmp('qTitleCombox').getValue();
		var qTitleName = Ext.getCmp('qTitleCombox').getRawValue();

		//根据选择的标题重新加载grid
        var newUrl = '${pageContext.request.contextPath}/qContentAction!getAllQContent.action?showInfoId='+type+'&titleId=' + qTitleID;
        store.proxy.setUrl(newUrl);
	    store.load();
	    //重新选择行
	    grid.getSelectionModel().clearSelections();
	    qTitleId = '';
    	showId = '';
    	data = null;
	}
	
	function openDialog(){
		Ext.useShims=true;
		 
	    win=new Ext.Window(
	    	    {
		    	    //title:"新增",
	        	    width:370,
	        	    height:220,
	        	    //html:updateDialogStr,
	        	    modal:true,
	        	    closeAction:"close",
	        	    items:[
							new Ext.FormPanel({
								id:"formPanel",
									frame:true,
									x:40,
							        y:30,
									labelAlign:"right",
									labelWidth: 85,
							        width:360,
							        height:400,
							        border:false,
							        waitMsgTarget: true,
							      //  region:"west",
							        reader : new Ext.data.JsonReader({
							        	//root:'showInfoList'
								        }, [
							            {name:'id',mapping:'id',type:'int'}, 
							            {name:'show_id',mapping:'show_id'},
							            {name:'Qtitle_id',mapping:'Qtitle_id'},
							            {name:'Qtext',mapping:'Qtext'}, 
							            {name:'isAvalible',mapping:'isAvalible',type:'int'}
							            ]),
							        items: [
							            new Ext.form.FieldSet({
							               	title: '问卷调查内容',
							               	width:340,
							               	autoHeight: true,
							                defaultType: 'textfield',
							                items: [{
							                        fieldLabel: 'ID',
							                        name: 'id',
							                        width:190,
							                        hidden:true
							                    },
							                    new Ext.form.ComboBox({
										   			   id:'show_id_checkbox',
										   			   hiddenName:"show_id",
										               fieldLabel: '展会名称',
										               store: showInfoStore,
										               valueField:'id',
										               displayField:'showName',
										               typeAhead: true,
										               mode: 'local',
										               triggerAction: 'all',
										               emptyText:'请选择展会',
										               selectOnFocus:false,
										               width:220 
										    	   	}),
							                	new Ext.form.ComboBox({
							                		   id:"Qtitle_id_checkbox",
							    		   			   hiddenName:'Qtitle_id',
							    		               fieldLabel: '标题名',
							    		               editable : false,
							    		               store: up_qTitleStore,
							    		               valueField:'id',
							    		               displayField:'Qtitle',
							    		               typeAhead: true,
							    		               mode: 'local',
							    		               triggerAction: 'all',
							    		               //emptyText:'选择',
							    		               selectOnFocus:true,
							    		               width:190 
							    		    	   	}),
							                    {
							                        fieldLabel: '选择项名',
							                        //emptyText:'如123456@qq.com',
							                        name: 'Qtext',
							                        allowBlank: false,
							                        width:190
							                    },
							                    new Ext.form.ComboBox({
							                    fieldLabel: '是否可用',
							                    hiddenName:'isAvalible',
							                    allowBlank:false,
							                    editable : false,
							                    blankText:'不能为空',
							                    store: new Ext.data.ArrayStore({
							                        fields: ['abbr', 'isAvalible'],
							                        data : [
								                            [0,'否'],
								                            [1,'是']] 
							                    }),
							                    valueField:'abbr',
							                    displayField:'isAvalible',
							                    typeAhead: true,
							                    mode: 'local',
							                    triggerAction: 'all',
							                    selectOnFocus:true,
							                    width:190
							                })
							                ]
							            })
							        ]
							})
		   	        	    ],
	        	    buttons:[{
	        	    	text:"确认",handler:function(){
	        	    	saveQTitle();
	        	    }}, {
		        	    text:"取消",handler:function(){
	        	    	win.close();
	        	    	}
			       	}]
	        	}); 
	    win.show();
	    
	    //切换展会查询问卷标题
		Ext.getCmp("show_id_checkbox").on('select',function(combo,record,index){
			//当前选择的机构类的id
			var showId = Ext.getCmp('show_id_checkbox').getValue();
			//根据选择的展会加载相应的标题
  	       var newUrl = '${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action?showId='+showId;
  	       up_qTitleStore.proxy.setUrl(newUrl);
  	   	   up_qTitleStore.load();
  	       
  	       //TODO:重新选择标题
  	       Ext.getCmp('Qtitle_id_checkbox').setValue("");
		});
		//点击右上角的关闭按钮后
	    win.on("close",function(){
		});
	}
	
  		function InitMain(){
  			
  			//复选框
			var sm = new Ext.grid.CheckboxSelectionModel({checkOnly:false});

  			var cm = new Ext.grid.ColumnModel([
  			                              sm,
   	  	                 	  	          {
   	  	              	  	                  id       :'id',
   	  	              	  	                  header   : 'ID', 
   	  	              	  	                  width    : 50, 
   	  	              	  	                  sortable : true, //排序
   	  	              	  	              	 // hidden : true,//隐藏列
   	  	              	  	                  dataIndex: 'id'
   	  	              	  	              },{
   	  	              	  	                  header   : 'show_id', 
   	  	              	  	                  width    : 55, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  hidden   :true,
   	  	              	  	                  dataIndex: 'show_id'
   	  	              	  	              },{
   	  	              	  	                  header   : '展会名称', 
   	  	              	  	                  width    : 150, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  dataIndex: 'showName'
   	  	              	  	              },{
   	  	              	  	                  header   : '标题名', 
   	  	              	  	                  width    : 75, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  dataIndex: 'Qtitle'
   	  	              	  	              },
   	  	              	  	              {
   	  	              	  	                  header   : '选择项名', 
   	  	              	  	                  width    : 95, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  dataIndex: 'Qtext'
   	  	              	  	              },{
   	  	              	  	                  header   : '是否可用', 
   	  	              	  	                  width    : 60, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  renderer : isVailabel, 
   	  	              	  	                  dataIndex: 'isAvalible'
   	  	              	  	              },{
	   	  	              	  	         	  header   : 'Qtitle_id', 
		              	  	                  width    : 250, 
		              	  	                  sortable : true,
		              	  	                  hidden   : true,
		              	  	                  dataIndex: 'Qtitle_id'
   	  	              	  	              }
   	  	              	  	          ]);

    	  	var record = Ext.data.Record.create([
    	  	               				{name: 'id', type: 'int'},
    	  	               				{name: 'show_id'},
    	  	               				{name: 'showName'},
    	  	          					{name: 'Qtitle'},
    	  	          					{name: 'Qtext'},
    	  	          					{name: 'isAvalible'},
    	  	          					{name: 'Qtitle_id'}
    	  	            	  	          ]);
  	          var reader = new Ext.data.JsonReader({
  	  	          root:'qList'
  	  	          },record);

    	 // 	var proxy = new Ext.data.MemoryProxy(audienceArr);
    	 	//加载全问的内容
    	 	proxy = new Ext.data.HttpProxy({
        	 	url:'${pageContext.request.contextPath}/qContentAction!getAllQContent.action?',
        	 	method:'post'
        	});

  	  	      store = new Ext.data.Store({
  	  	          proxy:proxy,
  	  	          reader:reader
  	  	      });
  	  	  	  store.load({});

			  //用户是否可用
	  	  	  function isVailabel(val) {
	  	        if (val == 0) {
	  	            return '否';
	  	        } else if (val == 1) {
	  	            return '是';
	  	        }
	  	        return val;
	  	      }

  	  	      // create the Grid
  	  	      grid = new Ext.grid.GridPanel({
  	  	          store: store,
  	  	     	  cm: cm,
  	  	          stripeRows: true,
  	  	          autoExpandColumn: 'id',
  	  	          width: "100%",
	  	  	      sm:sm,
	  			  viewConfig: {
	  				  forceFit: true
	  			  },
  	  	          stateful: true,
  	  	          stateId: 'grid',
  	  	          region:"north"
  	  	      });
  	  	  //点击选中行，修改
	 	  grid.getSelectionModel().on('rowselect', function(sm, rowIdx, r){
	        	qTitleId = r.data.id;
	        	showId = r.data.show_id;
	        	//alert("id:" + Ext.util.JSON.encode(r.data));
	        	data = r.data;
	        	//根据选择行的show_id加载up_qTitleStore
	  		    up_qTitleStore = new Ext.data.Store({
	  				  proxy:new Ext.data.HttpProxy({url:'${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action?showId='+showId}),
	  				  reader:new Ext.data.JsonReader({root:'qTitleList'},['id','Qtitle'])  //显示的字段名，与服务器端返回的json中字段一致
	  				  });
	  		    up_qTitleStore.load();
	    	});

  	  	      
  	  	  var centerPanel = new Ext.Panel({
	  	  		id:"centerPanel",
	  	  		//title:"问卷调查风容",
	  	  		layout:"fit",
	  	  		region:"center",
	  	  		//width:"75%",
	  	  		items:[
						grid
	   	  	  		]
	  	  		
	  		});
		//调查问卷标题信息
	  	var qTitleStore = new Ext.data.Store({
			  proxy:new Ext.data.HttpProxy({url:'${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action?type='+type}),
			  reader:new Ext.data.JsonReader({root:'qTitleList'},['id','Qtitle'])  //显示的字段名，与服务器端返回的json中字段一致
			  });
	  	qTitleStore.load(); 
	  	up_qTitleStore = new Ext.data.Store({
			  proxy:new Ext.data.HttpProxy({url:'${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action?showId='+showId}),
			  reader:new Ext.data.JsonReader({root:'qTitleList'},['id','Qtitle'])  //显示的字段名，与服务器端返回的json中字段一致
			  });
	   up_qTitleStore.load();
	  	
	  	showInfoStore = new Ext.data.Store({
			  proxy:new Ext.data.HttpProxy({url:'${pageContext.request.contextPath}/showInfoManagerAction!getAllShowInfo.action'}),
			  reader:new Ext.data.JsonReader({root:'showInfoList'},['id','showName'])  //显示的字段名，与服务器端返回的json中字段一致
			  });
	  	showInfoStore.load(); 

		//机构类型 表单
		var qTitleForm = new Ext.FormPanel({
			frame:true,
			labelAlign:"right",
			labelWidth: 90,
	        width:340,
	        border:false,
	        waitMsgTarget: true,
	        region:"north",
	        layout:"form",
			items:[
		    	   	
		    	   	new Ext.form.ComboBox({
		   			   id:'showInfoCombox',
		   			   name:'showInfo',
		               fieldLabel: '展会名称',
		               hiddenName:'state',
		               store: showInfoStore,
		               valueField:'id',
		               displayField:'showName',
		               typeAhead: true,
		               mode: 'local',
		               triggerAction: 'all',
		               emptyText:'请选择',
		               selectOnFocus:false,
		               width:240 
		    	   	}),
		    	   	new Ext.form.ComboBox({
			   			   id:'qTitleCombox',
			   			   name:'qtitle',
			               fieldLabel: '调查问卷标题',
			               hiddenName:'state',
			               store: qTitleStore,
			               valueField:'id',
			               displayField:'Qtitle',
			               typeAhead: true,
			               mode: 'local',
			               triggerAction: 'all',
			               emptyText:'请选择展会',
			               selectOnFocus:true,
			               width:240 
			    	   	})
			       ]
		});
		//切换展会查询问卷标题
		Ext.getCmp("showInfoCombox").on('select',function(combo,record,index){
			//当前选择的机构类的id
			var showId = Ext.getCmp('showInfoCombox').getValue();
			var qTitleName = Ext.getCmp('qTitleCombox').getRawValue();

			//根据选择的展会加载相应的标题
  	       var newUrl = '${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action?showId='+showId;
  	       qTitleStore.proxy.setUrl(newUrl);
  	       qTitleStore.load();

  	       //重置标题内容
  	       Ext.getCmp('qTitleCombox').setValue("");
  	       var newUrl = '${pageContext.request.contextPath}/qContentAction!getAllQContent.action?titleId=';
	       //alert(newUrl);
	       store.proxy.setUrl(newUrl);
		   store.load();
		   //重置选择行
		   qTitleId = "";
		});
		//切换标题查询内容
		Ext.getCmp("qTitleCombox").on('select',function(combo,record,index){
			//当前选择的机构类的id
			var qTitleID = Ext.getCmp('qTitleCombox').getValue();
			var qTitleName = Ext.getCmp('qTitleCombox').getRawValue();

			//根据选择的标题重新加载grid
  	       var newUrl = '${pageContext.request.contextPath}/qContentAction!getAllQContent.action?titleId=' + qTitleID;
  	       store.proxy.setUrl(newUrl);
  		   store.load();
  		   //重置选择行
  		   qTitleId = "";

		});
		
		 //设置默认值 
        //Ext.getCmp("qTitleCombox").setValue('0');
		
		var northPanel = new Ext.Panel({
			id:"northPanel",
			region:"north",
			//split:"true",
			layout:"fit",
			height:90,
			items:[
				qTitleForm
			],
			tbar:[//定义顶部工具栏
	 				 {
	         	      id:'addnode',
	                   text: '新增',
	                   iconCls:'add',
	                   //iconCls : 'edit',
	                   //pressed : true,
	                   handler : function(){
	                	//加载全部up_qTitleStore
			  		    up_qTitleStore = new Ext.data.Store({
			  				  proxy:new Ext.data.HttpProxy({url:'${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action'}),
			  				  reader:new Ext.data.JsonReader({root:'qTitleList'},['id','Qtitle'])  //显示的字段名，与服务器端返回的json中字段一致
			  				  });
			  		    up_qTitleStore.load();
			  	   	    
	 					openDialog();
 				   		win.setTitle("新增");
	                   }
	                  },'-',{
	         	      id:'updatenode',
	                   text: '修改',
	                   iconCls : 'option',
	                   //pressed : true,
	                   handler : function(){
	                	  if(qTitleId == ""){
		                	  Ext.Msg.alert("提示","请选择修改行");
	                	  }else{
		                   openDialog();
		                   win.setTitle("修改");
		                   Ext.getCmp("formPanel").form.loadRecord(new Ext.data.MemoryProxy(data));
	                	  }
	                   }
	                  },'-',{
	         	      id:'deletenode',
	                   text: '删除',
	                   iconCls : 'remove',
	                   //pressed : true,
	                   handler : function(){
	                	   var obArr = sm.getSelections();
	                   	   var selIDStr = "";
	                   	   if(obArr.length == 0){
		                   	   Ext.Msg.alert("提示","没有选中行");
		                   	   return;
	                   	   }
	                   	   for(var i=0;i<obArr.length;i++){
		                   	   if(selIDStr > 0){
		                   			selIDStr += ",";
		                   	   }
	                   			selIDStr += obArr[i].id;
	                   	   }
	                	   deleteTermQ(selIDStr);
	                   }
	                  }]
		});
  	  	var viewport = new Ext.Viewport({
			id : 'viewprot',
			layout : 'border',
			items : [
					centerPanel,
					northPanel
				]
		});
  	}
  		Ext.onReady(InitMain); 
  	
  	
  	</script>
  </body>
</html>
